<template>
    <div class="wrapper">
        <div style="margin: 100px auto; background-color: #fff; width: 400px; height: 500px; padding: 20px; border-radius: 10px">
            <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>注 册</b></div>
            <el-form :model="user" :rules="rules" ref="userForm">
                <el-form-item prop="username">
                    <el-input placeholder="请输入账号" size="medium" style="margin: 5px 0" prefix-icon="el-icon-user"
                              v-model="user.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock"
                              show-password v-model="user.password"></el-input>
                </el-form-item>
                <el-form-item prop="confirmPassword">
                    <el-input placeholder="请确认密码" size="medium" style="margin: 5px 0" prefix-icon="el-icon-lock"
                              show-password v-model="user.confirmPassword"></el-input>
                </el-form-item>
                <el-form-item prop="email">
                    <el-input placeholder="请输入qq邮箱" size="medium" style="margin: 5px 0"
                              prefix-icon="el-icon-ali-youxiang"
                              v-model="user.email"></el-input>
                </el-form-item>
                <el-form-item prop="formCode">
                    <el-input auto-complete="off"
                              v-model="user.formCode"
                              placeholder="验证码" style="width: 40%"
                              prefix-icon="el-icon-ali-yanzhengyanzhengma">
                    </el-input>
                    <el-button
                            type="primary"
                            style="margin-left: 10px;background-color: #FFCA22;border: none;color: #676767;"
                            @click="send" :disabled="!show">
                        <span v-show="show">发送验证码</span>
                        <span v-show="!show" class="count">{{count}} s</span>
                    </el-button>
                </el-form-item>
                <el-form-item>
                    <div style="text-align: center;">
                        <el-button class="login-btn" type="primary" native-type="submit" size="small"
                                   autocomplete="off" @click.native.prevent="register" round>注册
                        </el-button>
                    </div>
                    <div style="text-align: center;">
                        <el-link type="primary" @click="$router.push('/')" style="margin-left: 0px;">返回登录</el-link>
                        <!-- 				<el-button class="login-btn" type="primary" size="small"
                                  autocomplete="off" @click="$router.push('/')" round>返回登录</el-button> -->
                    </div>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import {sendEmail, saveOrUpdate} from '../api/administrator.js'

    const TIME_COUNT = 60
    export default {
        name: "Login",
        data() {
            return {
                user: {},
                validateCode: '',
                count: '',
                show: true,
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                    ],
                    confirmPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                    ],
                    formCode: [{required: true, trigger: 'blur', message: '验证码不能为空'}],
                    email: [
                        {required: true, message: '请输入邮箱地址', trigger: 'blur'},
                        {
                            type: 'email',
                            message: '请输入正确的邮箱地址',
                            trigger: ['blur', 'change'],
                        },
                    ]
                },
            }
        },
        methods: {
            register() {
                this.$refs['userForm'].validate(async (valid) => {
                    if (valid) {  // 表单校验合法
                        if (this.user.password !== this.user.confirmPassword) {
                            this.$message.error("两次输入的密码不一致")
                            return false
                        }
                        if (this.validateCode != this.user.formCode) {
                            this.$message.error("验证码错误！")
                            return false
                        }
                        await saveOrUpdate(this.user).then(res => {
                            if (res.data != '0') {
                                this.$message.success("注册成功,请等待管理员审核！")
                                this.$router.push('/')
                            } else {
                                this.$message.error("用户名已存在!")
                            }
                        }).catch(err => {
                            this.$message.error('请求出错了:' + err)
                        })
                    }
                });
            },
            async send() {
                this.$refs['userForm'].validateField('email', async validateError => {
                    if (!validateError) {
                        // 验证码倒计时
                        if (!this.timer) {
                            this.count = TIME_COUNT
                            this.show = false
                            this.timer = setInterval(() => {
                                if (this.count > 0 && this.count <= TIME_COUNT) {
                                    this.count--
                                } else {
                                    this.show = true
                                    clearInterval(this.timer)
                                    this.timer = null
                                }
                            }, 1000)
                        }
                        await sendEmail(this.user.email).then(res => {
                            if (res.data == 'error') {
                                this.$message.error("出现错误！")
                            } else {
                                this.validateCode = res.data
                                console.log(this.validateCode)
                            }
                        }).catch(err => {
                            this.$message.error("出错了：" + err)
                        })
                    }
                })
            }
        },
        watch: {
            user(newVal, oldVal) {
                console.log(newVal.username + "----" + oldVal.username)
            }
        }
    }
</script>

<style scoped>
    .wrapper {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        background: url("../assets/login/background.jpg") no-repeat center
        /* background-color: #333; */
    }

</style>
